package com.tky.conference.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.tky.conference.R;

/**
 * Created by cll on 2015/12/14.
 */
public class MainFragment extends Fragment{
    private PagerSlidingTabStrip mPagerSlidingTabStrip;
    private ViewPager mViewPager;
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        setUserVisibleHint(true);
        super.onActivityCreated(savedInstanceState);
    }

    /**
     * 定义布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_labs,null);
        initPager(view);
        initTabsValue();
        return view;
    }

    public void turnToNextItem(int position) {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(position);
        }
    }

    /**
     * 初始化pager
     * @param view
     */
    public void initPager(View view) {
        mPagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        //取消预加载
        mViewPager.setOffscreenPageLimit(10);
        String[] titles = {"我组织的会议", "我负责的会议", "我参与的会议", "我的会议记录"};
        mViewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager(), titles));
        mViewPager.setCurrentItem(0);
        mPagerSlidingTabStrip.setViewPager(mViewPager);

        mPagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            /**
             * 页面滑动的状态
             * @param arg0
             */
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    /**
     * mPagerSlidingTabStrip默认值配置
     */
    private void initTabsValue() {
        // 底部游标颜色
        mPagerSlidingTabStrip.setIndicatorColor(getResources().getColor(R.color.bottom_color));
        // tab的分割线颜色
        mPagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT);
        // tab背景
        mPagerSlidingTabStrip.setBackgroundColor(Color.parseColor("#009688"));
        // tab底线高度
        mPagerSlidingTabStrip.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                1, getResources().getDisplayMetrics()));
        // 游标高度
        mPagerSlidingTabStrip.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                5, getResources().getDisplayMetrics()));
        // 选中的文字颜色
        //mPagerSlidingTabStrip.setSelectedTextColor(Color.WHITE);
        // 正常文字大小
        int dms = (int) getResources().getDimension(R.dimen.title);
        mPagerSlidingTabStrip.setTextSize(dms);
        mPagerSlidingTabStrip.setTextColor(getResources().getColor(R.color.default_text_color));
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private String[] titles;

        public MyPagerAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            this.titles = titles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
                case 0:
                    f = ZZFragment.newInstance();
                    break;
                case 1:
                    f = DutyFragment.newInstance();
                    break;
                case 2:
                    f = JoinFragment.newInstance();
                    break;
                case 3:
                    f = RecordFragment.newInstance();
                    break;
                default:
                    break;
            }
            return f;
        }
    }
}
